<?php

class BGoodsOrder extends BaseModel
{
    protected $res;
    protected $key = "admin_";

    public function getSource()
    {
        return "f_b_goods_order";
    }

    public function initialize()
    {
        parent::initialize();
    }


    public function order_list($params = [])
    {
        $list = [];
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $where = '1=1 ';
        if (isset($params['phone']) && $params['phone']) {
            $where .= ' and u.phone like %' . $params['phone'] . '%';
        }
        if (isset($params['out_trade_no']) && $params['out_trade_no']) {
            $where .= ' and o.out_trade_no =' . $params['out_trade_no'];
        }
        $order = 'o.created_at DESC';
        $sql   = <<<EOT
                SELECT
                    u.real_name,
                    u.phone,
                    o.id,
                    o.order_status,
                    o.goods_count,
                    o.goods_amount_total,
                    o.order_amount_total,
                    o.order_deduct_total,
                    o.order_contribution_total,
                    o.order_use_discount_points_total,
                    o.order_use_cash_points_total,
                    o.orderlogistics_id,
                    o.address_id,
                    o.pay_channel,
                    o.out_trade_no,
                    o.order_remark,
                    o.remark,
                    o.created_at,
                    o.pay_at,
                    o.delivery_at 
                FROM
                    BGoodsOrder AS o
                    LEFT JOIN User AS u ON u.id = o.user_id 
                WHERE
                    {$where}
                ORDER BY
                    {$order}
                    LIMIT {$page},{$size}
                      
EOT;
        /**
         * @see ManagerInterface::executeQuery
         */

        $res = $this->modelsManager->executeQuery($sql);
        if ($res) {
            $list = $res->toarray();
        }
        return $list;
    }


    public function order_count($params = [])
    {

        $where = '1=1 ';
        if (isset($params['phone']) && $params['phone']) {
            $where .= ' and u.phone like %' . $params['phone'] . '%';
        }
        if (isset($params['out_trade_no']) && $params['out_trade_no']) {
            $where .= ' and o.out_trade_no =' . $params['out_trade_no'];
        }

        $sql = <<<EOT
                SELECT
                    count(o.id) as total
                FROM
                    BGoodsOrder AS o
                    LEFT JOIN User AS u ON u.id = o.user_id
                WHERE
                    {$where}
                limit 1

EOT;

        /**
         * @see ManagerInterface::executeQuery
         */
        $res = $this->modelsManager->executeQuery($sql);

        $list = [];
        if ($res) {
            $list = $res->toarray();
        }

        return isset($list[0]['total']) ? $list[0]['total'] : 0;;
    }

    public function order_details($params = '1=1', $columns = '*')
    {
        $list   = $this::findFirst(
            array(
                $params,
                'columns' => $columns,
            ));
        $result = [];
        if ($list) {
            $result = $list->toarray();
        }
        return $result;
    }

    /**
     * @func 获取详情
     * @param $id
     * @param $switch
     * @return Mixed
     */
    public function find_by_id($id, $switch = false)
    {
        if (empty($id)) {
            return [];
        }
        $info = $this->findFirstById($id);
        if ($switch) {
            return $info;
        }
        if ($info) {
            return $info->toarray();
        } else {
            return [];
        }
    }

}
